package name.jasonfrank.clicktester;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Toast;

public class ClickTesterActivity extends Activity 
                                 implements OnClickListener, OnLongClickListener {
	
	//View view;
	int a;
	
	/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {

  	super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Set click and long-click listeners
    View button1 = findViewById(R.id.button1); // <Button> is a subclass of <View>
    button1.setOnClickListener(this);
    button1.setOnLongClickListener(this);
  
    View redblock = findViewById(R.id.redblock);
    redblock.setOnClickListener(this);
    redblock.setOnLongClickListener(this);
    
    View greenblock = findViewById(R.id.greenblock);
    greenblock.setOnClickListener(this);
    greenblock.setOnLongClickListener(this);
    
    View yellowblock = findViewById(R.id.yellowblock);
    yellowblock.setOnClickListener(this);
    yellowblock.setOnLongClickListener(this);
    
    View blueblock = findViewById(R.id.blueblock);
    blueblock.setOnClickListener(this);
    blueblock.setOnLongClickListener(this);
    
  } //// end onCreate() //////////////

  
	@Override
	public void onClick(View v) {
		
		String widgetStr = whichWidget(v);
		Toast.makeText(this, getString(R.string.shortclick) + " " + widgetStr,
				           Toast.LENGTH_SHORT).show();
	}  
  
	@Override
	public boolean onLongClick(View v) {
		
		String widgetStr = whichWidget(v);

		
		// If it wasn't the button, initiate the change color menu
		if(widgetStr != getString(R.string.buttonString)) {
			processMenu(v);
		}
		// if it was the button
		else {
			Toast.makeText(this,getString(R.string.longclick) + " " + widgetStr,
					           Toast.LENGTH_SHORT).show();
		}
		
		return true;
	}

	
	// Method to determine which widget was pressed
	private String whichWidget(View v) {
		
		String widgetStr = "";
		
		switch(v.getId()) {
			case R.id.button1:
				widgetStr = getString(R.string.buttonString);
				break;
			case R.id.redblock:
				widgetStr = getString(R.string.redString);
				break;
			case R.id.greenblock:
				widgetStr = getString(R.string.greenString);
				break;
			case R.id.yellowblock:
				widgetStr = getString(R.string.yellowString);
				break;
			case R.id.blueblock:
				widgetStr = getString(R.string.blueString);
				break;
		}
		return widgetStr;
	} /////// end whichWidget() //////////

  
	// Menu with list of color options
	private void doMenu(View v, int index) {
		
		switch(index) {
		
			case 0:  // Android White
				v.setBackgroundColor(Color.WHITE);
				break;
			case 1:  // Android Black
				v.setBackgroundColor(Color.BLACK);
				break;
			case 2:  // Android Gray
				v.setBackgroundColor(Color.GRAY);
				break;
			case 3:  // Original red, user-defined in colors.xml
				v.setBackgroundColor(Color.parseColor(getString(R.color.redback)));
				break;
			case 4:  // Original green, user-defined in colors.xml
				v.setBackgroundColor(Color.parseColor(getString(R.color.greenback)));
				break;
		}
		
	} ////////// end doMenu() /////////////
  
	// Open AlertDialog holding color options menu 
	//  and process with anonymous inner class
	private void processMenu(final View v) {
		// Dr. Guidry's way:
					//		new AlertDialog.Builder(this).setTitle(R.string.processMenu_title)
					//		  .setItems(R.array.colornames,
					//		  		      new DialogInterface.OnClickListener() {
					//									@Override
					//									public void onClick(DialogInterface dialog, int which) {
					//										doMenu(v, which);
					//									}
					//								}).show();
		// --------------------------------
		// My way:
		
		// Get a new alert dialog and set its parameters.
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle(R.string.processMenu_title);
		// make an appropriate listener object to pass to setItems() below 
		DialogInterface.OnClickListener myDialogListener = 
			new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					doMenu(v, which);
				}
			};
		// set the alert dialog's items to display and also pass a listener 
		//  to react to user input on that dialog
		alert.setItems(R.array.colornames, myDialogListener);
		alert.show();
		
	} /////// end processMenu() /////////////
  
} ////////////////////////// end class ////////////////////////